package com.ruoyi.project.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.domain.InsReagentOrderList;
import com.ruoyi.project.product.service.IInsReagentOrderListService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 试剂订单详细Controller
 *
 * @author taney
 * @date 2020-11-23
 */
@AllArgsConstructor
@RestController
@RequestMapping("/product/orderList")
public class InsReagentOrderListController extends BaseController {

    private final IInsReagentOrderListService iInsReagentOrderListService;

    /**
     * 查询试剂订单详细列表
     */
    @PreAuthorize("@ss.hasPermi('product:orderList:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsReagentOrderList insReagentOrderList) {
        startPage();
        LambdaQueryWrapper<InsReagentOrderList> lqw = new LambdaQueryWrapper<InsReagentOrderList>();
        if (insReagentOrderList.getOrderId() != null) {
            lqw.eq(InsReagentOrderList::getOrderId, insReagentOrderList.getOrderId());
        }
        if (StringUtils.isNotBlank(insReagentOrderList.getMachineType())) {
            lqw.eq(InsReagentOrderList::getMachineType, insReagentOrderList.getMachineType());
        }
        if (StringUtils.isNotBlank(insReagentOrderList.getItemNo())) {
            lqw.eq(InsReagentOrderList::getItemNo, insReagentOrderList.getItemNo());
        }
        if (insReagentOrderList.getAmount() != null) {
            lqw.eq(InsReagentOrderList::getAmount, insReagentOrderList.getAmount());
        }
        if (StringUtils.isNotBlank(insReagentOrderList.getReagentSpec())) {
            lqw.eq(InsReagentOrderList::getReagentSpec, insReagentOrderList.getReagentSpec());
        }
        lqw.orderByAsc(InsReagentOrderList::getPriority);
        List<InsReagentOrderList> list = iInsReagentOrderListService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出试剂订单详细列表
     */
    @PreAuthorize("@ss.hasPermi('product:orderList:export')")
    @Log(title = "试剂订单详细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsReagentOrderList insReagentOrderList) {
        LambdaQueryWrapper<InsReagentOrderList> lqw = new LambdaQueryWrapper<InsReagentOrderList>(insReagentOrderList);
        List<InsReagentOrderList> list = iInsReagentOrderListService.list(lqw);
        ExcelUtil<InsReagentOrderList> util = new ExcelUtil<InsReagentOrderList>(InsReagentOrderList.class);
        return util.exportExcel(list, "orderList");
    }

    /**
     * 获取试剂订单详细详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:orderList:query')")
    @GetMapping(value = "/{orderListId}")
    public AjaxResult getInfo(@PathVariable("orderListId") Long orderListId) {
        return AjaxResult.success(iInsReagentOrderListService.getById(orderListId));
    }

    /**
     * 新增试剂订单详细
     */
    @PreAuthorize("@ss.hasPermi('product:orderList:add')")
    @Log(title = "试剂订单详细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsReagentOrderList insReagentOrderList) {
        iInsReagentOrderListService.save(insReagentOrderList);
        return AjaxResult.success(insReagentOrderList);
    }

    /**
     * 修改试剂订单详细
     */
    @PreAuthorize("@ss.hasPermi('product:orderList:edit')")
    @Log(title = "试剂订单详细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsReagentOrderList insReagentOrderList) {
        return toAjax(iInsReagentOrderListService.updateById(insReagentOrderList) ? 1 : 0);
    }

    /**
     * 删除试剂订单详细
     */
    @PreAuthorize("@ss.hasPermi('product:orderList:remove')")
    @Log(title = "试剂订单详细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderListIds}")
    public AjaxResult remove(@PathVariable Long[] orderListIds) {
        return toAjax(iInsReagentOrderListService.removeByIds(Arrays.asList(orderListIds)) ? 1 : 0);
    }

    /**
     * 删除试剂订单详细
     */
    @PreAuthorize("@ss.hasPermi('product:delOrderListByOrderId:remove')")
    @Log(title = "试剂订单详细", businessType = BusinessType.DELETE)
    @DeleteMapping("/delOrderListByOrderId/{orderId}")
    public AjaxResult delOrderListByOrderId(@PathVariable Long orderId) {
        LambdaQueryWrapper<InsReagentOrderList> lqw = new LambdaQueryWrapper<InsReagentOrderList>();
        lqw.eq(InsReagentOrderList::getOrderId, orderId);
        iInsReagentOrderListService.remove(lqw);
        return AjaxResult.success(orderId);
    }
}
